package policy.branches;

import policy.nodes.Node;

public class IPBranch extends Branch {
	public static final String IP = "IP";

	protected IPBranch(String ip, Node node) {
		super(ip, node);
	}

	public boolean equals(Object other) {
		if (other instanceof IPBranch) {
			IPBranch o = (IPBranch) other;
			return o.getValue().equals(value);
		}
		return false;
	}
	
	public int compareTo(Branch other) {
		IPBranch o = (IPBranch) other;

		// if both are network addresses, we don't care about the order
		if (!(value.contains("*")) && !(o.getValue().contains("*")))
			return 0;

		String[] parts = value.split("\\.");
		String[] oParts = o.getValue().split("\\.");

		// this check allows for values like *.*.*.* or 123.123.*.*
		for (int i = 0; i < parts.length; i++) {
			// the IPs are exclusive so order doesn't matter
			if (parts[i].equals("*"))
				return 1;
			if (oParts[i].equals("*"))
				return -1;
			if (!parts[i].equals(oParts[i]))
				return 0;
			// this ip is more general than the other one
		}
		return -1; // this IP is more specific than the other one
	}
}
